define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'auth/asbitemplate/index' + location.search,
                    add_url: 'auth/asbitemplate/add',
                    edit_url: 'auth/asbitemplate/edit',
                    del_url: 'auth/asbitemplate/del',
                    multi_url: 'auth/asbitemplate/multi',
                    import_url: 'auth/asbitemplate/import',
                    table: 'auth_asbi_template',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                visible: false,
                showToggle: false,
                showColumns: false,
                search:false,
                showExport: false,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'template_name', title: __('Template_name'), operate: 'LIKE'},
                        // {field: 'salary_benchmark_id', title: __('Salary_benchmark_id')},
                        // {field: 'salary_benchmark_item_id', title: __('Salary_benchmark_item_id')},
                        // {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            // 附件归类
            $(document).on('change', '.benchmark', function () {
                var adress = $(this);
                Fast.api.ajax({
                    url: "auth/asbitemplate/benchmark_item_list",
                    loading: false,
                    type: "post",
                    data: {
                        salary_benchmark_id:adress.val()
                    },
                }, function (data) {
                    var html = [];
                    var defaultvalue = 1;
                    for (var i = 0; i < data.length; i++) {
                        html.push("<option value='" + data[i].id + "' " + (defaultvalue == data[i].name ? "selected" : "") + " data-subtext='" + data[i].title + "'>" + data[i].name + "</option>");
                    }
                    adress.next(".salary_benchmark_item_id").html(html);
                    return false; // 关闭成功的提示

                }, function () {
                    alert(23);
                });
            });


            $('#c-salary_benchmark_id').data('params',function () {
                return  {custom: {count_type:2}};
            });
            $('#c-salary_benchmark_item_id').data('params',function () {
                return  {custom: {salary_benchmark_id: $('#c-salary_benchmark_id').val()}};
            });
            Controller.api.bindevent();
        },
        edit: function () {

            // 附件归类
            $(document).on('change', '.benchmark', function () {
                var adress = $(this);
                Fast.api.ajax({
                    url: "auth/asbitemplate/benchmark_item_list",
                    loading: false,
                    type: "post",
                    data: {
                        salary_benchmark_id:adress.val()
                    },
                }, function (data) {
                    var html = [];
                    var defaultvalue = 1;
                    for (var i = 0; i < data.length; i++) {
                        html.push("<option value='" + data[i].id + "' " + (defaultvalue == data[i].name ? "selected" : "") + " data-subtext='" + data[i].title + "'>" + data[i].name + "</option>");
                    }
                    adress.next(".salary_benchmark_item_id").html(html);
                    return false; // 关闭成功的提示

                }, function () {
                    alert(23);
                });
            });
            // 附件归类
            // $(document).on('click', '.salary_benchmark_item_id', function () {
            //     // alert(2)
            //     var adress = $(this);
            //     // adress.html("<option value='' data-subtext=''>请选择</option>");
            //     Fast.api.ajax({
            //         url: "auth/asbitemplate/benchmark_item_list",
            //         loading: false,
            //         type: "post",
            //         data: {
            //             type:2,
            //             salary_benchmark_item_id:adress.val()
            //         },
            //     }, function (data) {
            //         var html = [];
            //         var defaultvalue = 1;
            //         for (var i = 0; i < data.length; i++) {
            //             html.push("<option value='" + data[i].id + "' " + (defaultvalue == data[i].name ? "selected" : "") + " data-subtext='" + data[i].title + "'>" + data[i].name + "</option>");
            //         }
            //         adress.html(html);
            //         return false; // 关闭成功的提示
            //
            //     }, function () {
            //         // alert(23);
            //     });
            // });
            $('#c-salary_benchmark_id').data('params',function () {
                return  {custom: {count_type:2}};
            });
            $('#c-salary_benchmark_item_id').data('params',function () {
                return  {custom: {salary_benchmark_id: $('#c-salary_benchmark_id').val()}};
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
